package com.sita.friend.utils;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.AccountUtils;
import com.sita.bike.utils.LogUtils;
import com.sita.friend.db.DBManager;
import com.sita.friend.db.InviteMessgeDao;
import com.sita.friend.domain.InviteMessage;

import java.util.List;
import java.util.UUID;

/**
 * Created by hongyun on 2016/2/14.
 */
public class MessageUtils {
    private final static String TAG = MessageUtils.class.getSimpleName();

    private static InviteMessgeDao inviteMessgeDao;

    /**
     * 获取未读消息数
     */
    public static int getUnreadMsgCountTotal() {
        int unreadMsgCountTotal = 0;
        int chatroomUnreadMsgCount = 0;
        unreadMsgCountTotal = EMChatManager.getInstance().getUnreadMsgsCount();
        for (EMConversation conversation : EMChatManager.getInstance().getAllConversations().values()) {
            if (conversation.getType() == EMConversation.EMConversationType.ChatRoom)
                chatroomUnreadMsgCount = chatroomUnreadMsgCount + conversation.getUnreadMsgCount();
        }

        int inviteMsgCountTotal = getUnreadInviteMessageCount();

        return unreadMsgCountTotal - chatroomUnreadMsgCount + inviteMsgCountTotal;
    }

    public static int getUnreadInviteMessageCount() {
        if (inviteMessgeDao == null) {
            inviteMessgeDao = new InviteMessgeDao(GlobalContext.getGlobalContext());
        }
        return inviteMessgeDao.getUnreadMessagesCount();

    }

    public static void resetUnreadInviteMessageCount() {
        if (inviteMessgeDao == null) {
            inviteMessgeDao = new InviteMessgeDao(GlobalContext.getGlobalContext());
        }
        inviteMessgeDao.resetUnreadMessageCount();

    }

    public static void notifyNewInviteMessage(InviteMessage msg) {
        if (inviteMessgeDao == null) {
            inviteMessgeDao = new InviteMessgeDao(GlobalContext.getGlobalContext());
        }

        String username = msg.getFrom();
        List<InviteMessage> msgs = inviteMessgeDao.getMessagesList();
        for (InviteMessage inviteMessage : msgs) {
            if (inviteMessage.getFrom().equals(username) && inviteMessage.getStatus().equals(msg.getStatus())) {
                return;
            }
        }

        inviteMessgeDao.saveMessage(msg);
        if (msg.getStatus() == InviteMessage.InviteMesageStatus.BEINVITEED) {
            int count = inviteMessgeDao.getUnreadMessagesCount() + 1;
            inviteMessgeDao.saveUnreadMessageCount(count);
        }
//        // 提示有新消息
//        getNotifier().viberateAndPlayTone(null);
    }

    public static InviteMessage createFriendMessage(String username, String accountId, InviteMessage.InviteMesageStatus status) {

        InviteMessage msg = new InviteMessage();
        msg.setFrom(username);
        msg.setTime(System.currentTimeMillis());
        msg.setAccountId(accountId);
        msg.setStatus(status);


        String result = "";
        switch (status) {
            case BEAGREED:
                result = "同意了你的好友请求";
                break;
            case BEINVITEED:
                result = "请求加你为好友";
                break;
            case BEAPPLYED:
                break;
            case BEREFUSED:
                break;
            default:
                break;

        }
        LogUtils.d(TAG, username + result);

        return msg;
    }

    public static void createTxtMessage(String inviter, String id, String content, boolean isGroup) {
        EMMessage msg = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
        msg.setChatType(isGroup ? EMMessage.ChatType.GroupChat : EMMessage.ChatType.Chat);
        msg.setFrom(inviter);
        msg.setTo(id);
        msg.setMsgId(UUID.randomUUID().toString());
        msg.addBody(new TextMessageBody(content));
        // 保存邀请消息
        EMChatManager.getInstance().saveMessage(msg);
    }

    public static EMMessage createTxtMessageSingle(String from, String to, String content) {
        EMMessage msg = EMMessage.createSendMessage(EMMessage.Type.TXT);
        TextMessageBody body = new TextMessageBody(content);
        msg.addBody(body);
        msg.setTo(to);
        msg.setFrom(from);
        msg.setMsgTime(System.currentTimeMillis());
        return msg;
    }

    public static void sendTxtMessage(String to, String content) {
        //发送消息
        EMMessage message = EMMessage.createTxtSendMessage(content, to);
        EMChatManager.getInstance().sendMessage(message, new EMCallBack() {
            @Override
            public void onSuccess() {
                LogUtils.d(TAG, "send accept txt message ok");
            }

            @Override
            public void onError(int i, String s) {
                LogUtils.d(TAG, "send txt message failed");
            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }


}
